﻿@page "/"
@inject IUserAccountServices UserAccountServices
@inject NavigationManager NavigationManager
@inject ILocalStorageService LocalStorage


<MCard 
    Style="position: absolute; top: 50%;left: 50%;transform: translate(-50%, -50%);" 
    Width="800" 
    Height="500" 
    Elevation="10">
    <MContainer 
        Fluid 
        Style="height:100%; padding:0px">
        <MRow 
            Style="height:100%; border-radius:5px;position:relative" 
            NoGutters>
            <MCol 
                Style="width:50%;height:100%;position:relative;">
                <p style="margin-top:30px;display:flex;justify-content:center;font-family:'Microsoft JhengHei';font-size:x-large">长春市旅游信息服务平台</p>
                <MImage 
                    Src="imgs/logo.png" 
                    Style="position:absolute;left:21%;right:21%;top:120px;margin:auto;border-radius:50%"/>
            </MCol>
            <MDivider 
                Vertical 
                Inset 
                Style="position:absolute;left:50%;height:90%;top:5%;margin:0"/>
            <MCol>
                <p style="margin-top:30px;display:flex;justify-content:center;font-size:x-large;font-weight:200">系统登录</p>
                <div style="margin-top:20%;position:relative;">
                    <MTextField 
                        TValue="string" 
                        Style="width:80%;position:absolute;left:0;right:0;margin:auto;" 
                        Solo Label="账号"
                        Hint="注册时获取的账号，由数字组成" 
                        PrependInnerIcon="mdi-account-circle-outline"
                        Color="#4DB6AC" @bind-Value="Account"/>
                    <MTextField 
                        TValue="string" Style="width:80%;position:absolute;left:0;right:0;top:100px;margin:auto;" 
                        Solo Label="密码"
                        Hint="最少8位，最多20位" PrependInnerIcon="mdi-key" @bind-Value="Password"
                        AppendIcon="@(ShowPassword ? "mdi-eye" : "mdi-eye-off")"
                        Type="@(ShowPassword ? "text" : "password")" Color="#4DB6AC"
                        Counter="true" 
                        OnAppendClick="() => ShowPassword = !ShowPassword"/>
                    <p style="color:red; font-size:x-small;position:absolute;left:45px;right:0;top:180px;margin:auto;">@ErrorMessage</p>
                    <MButton 
                        Style="position:absolute;left:40%;right:40%;top:230px;margin:auto;color:white" 
                        Color="#4DB6AC" 
                        OnClick="LoginAsync">
                        登录
                    </MButton> 
                </div>
                <p style="position:absolute;bottom:20px;right:20px;font-size:x-small">没有账号？<a href="/register">立即注册</a></p>
            </MCol>
        </MRow>
    </MContainer>
</MCard>

@code {

    public string? Account { get; set; }

    public string? Password { get; set; }

    public bool ShowPassword { get; set; } = false;

    public string ErrorMessage { get; set; } = " ";

    private async Task LoginAsync()
    {
        ErrorMessage = string.Empty;
        int account;

        if (string.IsNullOrWhiteSpace(Account) || string.IsNullOrWhiteSpace(Password))
        {
            ErrorMessage = "账号或密码为空";
            return;
        }

        if (!int.TryParse(Account, out account))
        {
            ErrorMessage = "账号格式错误";
            return;
        }

        var loginResult = await UserAccountServices.LoginAsync(new User(account, Password));
        StateHasChanged();

        if (!loginResult.Succeed)
        {
            ErrorMessage = loginResult.Message;
            return;
        }
        await LocalStorage.SetItemAsync("user", loginResult.UserInfo);
        NavigationManager.NavigateTo("/index");
        return;
    }
}
